package com.winhxd.b2c.common.feign.customer;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.common.PagedCondition;
import com.winhxd.b2c.common.domain.customer.condition.DeliveryInfoCondition;
import com.winhxd.b2c.common.domain.customer.model.CustomerDeliveryInfo;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author lishaoping
 * @Description: 配送方式
 * @date 2019年1月7日15:13:46
 */
@FeignClient(value = ServiceName.CUSTOMER_SERVICE, fallbackFactory = DeliveryInfoServiceClientFallBack.class)
public interface DeliveryInfoServiceClient {

    /**
     * 保存或修改配送方式
     */
    @RequestMapping(value = "/customer/2100/v1/saveDeliveryInfo", method = RequestMethod.POST)
    ResponseResult<Boolean> saveDeliveryInfo(DeliveryInfoCondition condition);

    /**
     * 逻辑删除配送方式
     */
    @RequestMapping(value = "/customer/2101/v1/deleteDeliveryInfo", method = RequestMethod.POST)
    ResponseResult<Boolean> deleteDeliveryInfo(DeliveryInfoCondition condition);

    /**
     * 启用/禁用配送方式
     */
    @RequestMapping(value = "/customer/2102/v1/unusedDeliveryInfo", method = RequestMethod.POST)
    ResponseResult<Boolean> unusedDeliveryInfo(DeliveryInfoCondition condition);

    /**
     * (对外)查看所有配送方式(不分页)
     */
    @RequestMapping(value = "/customer/2103/v1/findDeliveryInfo", method = RequestMethod.POST)
    ResponseResult<List<CustomerDeliveryInfo>> findDeliveryInfo();

    /**
     * (对内)查看所有配送方式(分页)
     */
    @RequestMapping(value = "/customer/2104/v1/findDeliveryInfoTOPage", method = RequestMethod.POST)
    ResponseResult<PagedList<CustomerDeliveryInfo>> findDeliveryInfoTOPage(PagedCondition condition);

    /**
     * (查看所有配送方式ById
     */
    @RequestMapping(value = "/customer/2105/v1/findDeliveryInfoById/{id}", method = RequestMethod.GET)
    ResponseResult<CustomerDeliveryInfo> findDeliveryInfoById(@PathVariable("id") Long id);
}

@Component
class DeliveryInfoServiceClientFallBack implements DeliveryInfoServiceClient, FallbackFactory<DeliveryInfoServiceClient> {
    Logger logger = LoggerFactory.getLogger(ShoppingMallRuleServiceClientFallBack.class);
    Throwable throwable;

    @Override
    public DeliveryInfoServiceClient create(Throwable throwable) {
        this.throwable = throwable;
        return new DeliveryInfoServiceClientFallBack();
    }

    @Override
    public ResponseResult<Boolean> saveDeliveryInfo(DeliveryInfoCondition condition) {
        logger.error("DeliveryInfoServiceClientFallBack -> saveDeliveryInfo{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> deleteDeliveryInfo(DeliveryInfoCondition condition) {
        logger.error("DeliveryInfoServiceClientFallBack -> deleteDeliveryInfo{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Boolean> unusedDeliveryInfo(DeliveryInfoCondition condition) {
        logger.error("DeliveryInfoServiceClientFallBack -> unusedDeliveryInfo{}", throwable.getMessage());
        return new ResponseResult<Boolean>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<CustomerDeliveryInfo>> findDeliveryInfo() {
        logger.error("DeliveryInfoServiceClientFallBack -> findDeliveryInfo{}", throwable.getMessage());
        return new ResponseResult<List<CustomerDeliveryInfo>>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<PagedList<CustomerDeliveryInfo>> findDeliveryInfoTOPage(PagedCondition condition) {
        logger.error("DeliveryInfoServiceClientFallBack -> findDeliveryInfoTOPage{}", throwable.getMessage());
        return new ResponseResult<PagedList<CustomerDeliveryInfo>>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<CustomerDeliveryInfo> findDeliveryInfoById(Long id) {
        logger.error("DeliveryInfoServiceClientFallBack -> findDeliveryInfoById{}", throwable.getMessage());
        return new ResponseResult<CustomerDeliveryInfo>(BusinessCode.CODE_1001);
    }
}
